<?php

namespace App\Admin\Controllers;

use App\Chest;
use App\Grip;
use App\GripType;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class GripController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('格子管理')
            ->description('列表')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('格子管理')
            ->description('详情')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('格子管理')
            ->description('编辑')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('格子管理')
            ->description('新增')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {


        $grid = new Grid(new Grip());
        $chestNO = request()->get('chest_number');
        //通过get参数 实现关联查询
        $grid->model()->where(function ($query) use($chestNO) {
               $query->whereHas('chest', function ($query) use($chestNO) {
                    $query->where('chest_number', 'like',  '%' . $chestNO . '%');
                });
           }, '所属柜子');
        $grid->grip_number('格子编号');
        $grid->grip_name('格子名称');
        $grid->Chest()->chest_number('所属柜子');
        $grid->type()->type_name('类型');
        $grid->created_at('添加时间');
        $grid->updated_at('编辑时间');

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Grip::findOrFail($id));
        $show->id('Id');
        $show->grip_number('格子编号');
        $show->grip_name('格子名称');
        $show->Chest()->chest_number('所属柜子');
        $show->type()->type_name('类型');
        $show->created_at('添加时间');
        $show->updated_at('编辑时间');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Grip);
        $form->text('grip_number', '格子编号');
        $form->text('grip_name', '格子名称');
        $form->select('chest_id', '所属柜子')->options(Chest::getSelectOptions());
        $form->select('type_id', '柜子类型')->options(GripType::getSelectOptions());
        return $form;
    }
}
